package com.bw.greendao.dao;

import org.greenrobot.greendao.query.WhereCondition;

import java.lang.reflect.ParameterizedType;
import java.util.List;

/**
 * @author KK
 * @description:
 * @date :2021/10/20 9:17
 */
public class DaoImpl<T> implements DaoInterface<T> {

    private Class clazz;

    public DaoImpl() {
        //反射获取当前泛型类型
        ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
        clazz = (Class) type.getActualTypeArguments()[0];
    }

    @Override
    public boolean insert(T t) {
        return DaoSessionUtils.getInstance().getSession()
                .insertOrReplace(t)>0;
    }
    public List<T> selectAll(){
        List<T> list = DaoSessionUtils.getInstance().getSession().queryBuilder(clazz).list();
        return list;
    }
    public void updata(T t){
        DaoSessionUtils.getInstance().getSession().update(t);
    }

    @Override
    public T select(WhereCondition condition) {
        T t = (T) DaoSessionUtils.getInstance().getSession().queryBuilder(clazz).where(condition).build().unique();
        return t;
    }
}
